model carrefour
/*
 *  lam cho cac xe tranh nhau, chuyen lan duong
// * cai nay hoi phuc tap 
 * 
 */
global {
	file shape_file_lignes <- file('../includes/carrefour1/lignes.shp');
	file shape_file_enveloppes <- file('../includes/carrefour1/enveloppes.shp');
	file shape_file_terrains <- file('../includes/carrefour1/terrains.shp');
	file shape_file_departs <- file('../includes/carrefour1/departs.shp');
	file shape_file_arrivees <- file('../includes/carrefour1/arrivees.shp');
	file shape_file_feuxsignaux <- file('../includes/carrefour1/feuxsignaux.shp');
	int repulsion_strength min: 1 <- 10 ;
	int nb_vehicule <- 20;
	float min_speed <- 2.0;
	float max_speed <- 6.0;
	int size_of_vehicule <- 10;
	graph the_graph;
	int range_of_vehicules min:1 <- 10; 
//	set list_departs type:list of:depart <- list(depart);
//	list list_departs of:depart;
//	list list_arrivees of:arrivee;
//init
	
		
	init {
		create ligne from: shape_file_lignes;
		set the_graph <- as_edge_graph(list(ligne));
		create terrain from: shape_file_terrains;
		create depart from: shape_file_departs;
		create arrivee from: shape_file_arrivees;
//		set list_departs <- depart as list;
//		set list_arrivees <- arrivee as list;
			create vehicule number: nb_vehicule{
//				set location <- any_location_in( (graph)the_graph);
//cho nay fai chinh lai de ngau nhien xuat hien tren duong
				set location <- any_location_in( one_of(depart));
				set speed <- min_speed + rnd(max_speed - min_speed);
				set the_depart <- one_of (depart);
				set the_arrivee <- one_of (arrivee);
				set the_target <- any_location_in(the_arrivee);
		}
	}
	
	reflex creer_nouveaux_vehicules when:flip(0.5){
		create vehicule number: 1{
			set location <- any_location_in( one_of(depart));
				set speed <- min_speed + rnd(max_speed - min_speed);
				set the_depart <- one_of (depart);
				set the_arrivee <- one_of (arrivee);
				set the_target <- any_location_in(the_arrivee);
		}
	}
	
}
entities {
	
	species ligne  {
		rgb color <- rgb('red') ;
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	
	species terrain {
		string type;
		rgb color <- rgb('gray');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species depart {
		rgb color <- rgb('green');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species arrivee {
		rgb color <- rgb('blue');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species feusignal {
		rgb color <- rgb('black');
		aspect base {
			draw geometry: shape color: color;
		}
	}
	
	species vehicule skills:[moving] {
		rgb color <- rgb('yellow');
		depart the_depart <- nil;
		const range type: float <- float(range_of_vehicules);
//		depart the_depart <- one_of (list_departs);
		arrivee the_arrivee <- nil;
		int heading <- rnd(359);
		point the_target <- nil;
		
		reflex move when: the_target != nil {
			do goto target: the_target on: the_graph;
			switch the_target {
				match location {do action:die;}
			}
		}
		
		reflex flee_others {
			let close type: vehicule <- one_of (((self neighbours_at range ) of_species vehicule) sort_by (self distance_to each));
			if (close != nil){
				set heading <- (self towards close ) - 180 ;
				let dist <- self distance_to close;
				do move speed: repulsion_strength / dist heading: heading;
			}
		}
		
		aspect base {
			draw shape: circle color: color size:size_of_vehicule;
		}
	}
}

environment bounds: shape_file_enveloppes ;
	
experiment carrefour_05 type: gui {
	parameter 'Shapefile for the lignes:' var: shape_file_lignes category: 'GIS' ;
	parameter 'Shapefile for the terrains:' var: shape_file_terrains category: 'GIS' ;
	parameter 'Shapefile for the departs:' var: shape_file_departs category: 'GIS';
	parameter 'Shapefile for the arrivees:' var: shape_file_arrivees category: 'GIS';
	parameter 'Shapefile for the feux:' var: shape_file_feuxsignaux category: 'GIS';
	
	parameter 'Number of vehicules :' var:nb_vehicule category:'Vehicule';
	output {
		display carrefour_display refresh_every: 1 {
			species ligne aspect: base ;
			species terrain aspect: base;
			species depart aspect: base;
			species arrivee aspect: base;
//			species feusignal aspect: base;
			species vehicule aspect: base;
		}
	}
}